package com.qs.eggs.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.api.HttpApi;
import com.qs.eggs.view.activity.EvaluationActivity;
import com.qs.eggs.view.activity.PayMethodActivity;
import com.qs.eggs.view.ordermanager.activity.OrderManagerActivity;
import com.qs.eggs.view.ordermanager.activity.WaitEvaluationActivity;
import com.qs.eggs.view.ordermanager.activity.WaitPayActivity;
import com.qs.eggs.view.ordermanager.fragment.WaitPayFragment;
import com.qs.eggs.widgets.custom.CustomerListView;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder>  implements View.OnClickListener{
    private LayoutInflater mInflater;
    private List<Map<String, Object>> imagelist=new ArrayList<>();
    private Context context;
    private Handler mHandler;
    private List<Map<String, Object>>mlist = null;
    private PicAdapter picAdapter;
    public OrderAdapter(Context context, List<Map<String, Object>> imagelist, Handler mHandler) {
        mInflater = LayoutInflater.from(context);
        this.imagelist = imagelist;
        this.context=context;
        this.mHandler=mHandler;
    }
    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        private  TextView orderno,orderstate,allmoney,createtime,id,handler,delete;
        private  CustomerListView listView;
    }

    @Override
    public int getItemCount()
    {
        return imagelist.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = mInflater.inflate(R.layout.item_order,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        viewHolder.orderno = (TextView) view.findViewById(R.id.tv_order_number);
        viewHolder.orderstate = (TextView) view.findViewById(R.id.tv_order_state);
        viewHolder.allmoney = (TextView) view.findViewById(R.id.tv_allmoney);
        viewHolder.createtime = (TextView) view.findViewById(R.id.tv_createtime);
        viewHolder.listView=(CustomerListView)view.findViewById(R.id.listView);
        viewHolder.id=(TextView)view.findViewById(R.id.id);
        viewHolder.handler=(TextView)view.findViewById(R.id.tv_hander);
        viewHolder.delete=(TextView)view.findViewById(R.id.delete);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.handler.setVisibility(View.VISIBLE);
        viewHolder.id.setText(imagelist.get(position).get("id").toString());
        viewHolder.orderno.setText("订单号: "+imagelist.get(position).get("orderno")+"");
        viewHolder.allmoney.setText("共"+imagelist.get(position).get("totalcount")+"件商品、合计："+imagelist.get(position).get("totalmoney"));
        viewHolder.createtime.setText(imagelist.get(position).get("createtime").toString());

        HashMap<String,String> map=new HashMap<>();
        map.put("orderid",viewHolder.id.getText().toString());
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=searchMyOrderDetail")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("订单详情=="+s);
                        try {
                            mlist=new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject=jsonArray.getJSONObject(i);
                                Map<String,Object> map=new HashMap<>();
                                map.put("proid",jsonObject.getString("proid"));
                                map.put("price",jsonObject.getString("price"));
                                map.put("type",jsonObject.getString("type"));
                                map.put("proname",jsonObject.getString("proname"));
                                map.put("autoname",jsonObject.getString("autoname"));
                                map.put("count",jsonObject.getString("count"));
                                map.put("orderid",jsonObject.getString("orderid"));
                                mlist.add(map);
                            }
                            picAdapter = new PicAdapter(context, mlist);
                            viewHolder.listView.setAdapter(picAdapter);

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

        if(imagelist.get(position).get("orderstatus").equals("0")){
            viewHolder.delete.setVisibility(View.VISIBLE);
            viewHolder.orderstate.setText("待付款");
            viewHolder.handler.setText("立即付款");
            viewHolder.handler.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, PayMethodActivity.class);
                    intent.putExtra("orderno",imagelist.get(position).get("orderno").toString());
                    intent.putExtra("ordermoney",imagelist.get(position).get("totalmoney").toString());
                    intent.putExtra("biaoshi","2");
                    context.startActivity(intent);
                }
            });
            viewHolder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    delete(imagelist.get(position).get("id").toString());
                }
            });
        }else if (imagelist.get(position).get("orderstatus").equals("1")){
            viewHolder.orderstate.setText("待发货");
            viewHolder.handler.setVisibility(View.INVISIBLE);
            viewHolder.delete.setVisibility(View.INVISIBLE);
        }else if (imagelist.get(position).get("orderstatus").equals("2")){
            viewHolder.delete.setVisibility(View.INVISIBLE);
            viewHolder.orderstate.setText("待收货");
            viewHolder.handler.setText("确认收货");
            viewHolder.handler.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    confirmOrder(imagelist.get(position).get("id").toString());
                }
            });
        }else if (imagelist.get(position).get("orderstatus").equals("3")){
            viewHolder.delete.setVisibility(View.INVISIBLE);
            viewHolder.orderstate.setText("待评价");
            viewHolder.handler.setText("去评价");
            viewHolder.handler.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, EvaluationActivity.class);
                    intent.putExtra("orderid",imagelist.get(position).get("id").toString());
                    context.startActivity(intent);
                }
            });
        }else if (imagelist.get(position).get("orderstatus").equals("4")){
            viewHolder.orderstate.setText("订单完成");
            viewHolder.handler.setVisibility(View.INVISIBLE);
            viewHolder.delete.setVisibility(View.INVISIBLE);

        }else {

        }
    }

    //删除未付款的订单
    private void delete(String id){
        HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=deleteOrder")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if(s.equals("\"true\"")){
                            mHandler.sendEmptyMessage(5);
                        }else {
                            Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }

    //确认收货
    private void confirmOrder(String id){
        final HashMap<String,String> map=new HashMap<>();
        map.put("id",id);
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"myorder?action=confirmOrder")
                .params("data",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("确认收货=="+s);
                        if (s.equals("\"true\"")){
                              mHandler.sendEmptyMessage(5);

                        }
                    }
                });
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
